package com.wlstock.funi.Trader;

import android.content.Context;

import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.dao.PositionDao;
import com.wlstock.funi.dao.StrategyFundSelDao;
import com.wlstock.funi.fundenum.TRADE_ERROR_STATE;
import com.wlstock.funi.model.StrategyFundModel;
import com.wlstock.funi.utils.GlobalIndexHelper;
import com.wlstock.funi.utils.SharePreferenceUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wlstock.funi.Trader.ConfigUtils.KEY_MaxPosition;
import static com.wlstock.funi.Trader.ConfigUtils.KEY_MinIndex;
import static com.wlstock.funi.Trader.ConfigUtils.KEY_MinMoney;
import static com.wlstock.funi.fundenum.TRADE_ERROR_STATE.ERROR_BEYOND_BUYABLE_POSITION;
import static com.wlstock.funi.fundenum.TRADE_ERROR_STATE.ERROR_NOT_ENOUGH_MONEY;

/**
 * Created by admin on 16/9/18.
 */

public class CommonBuyConfig extends TraderConfiger implements TraderListener {



    private static CommonBuyConfig instance;
    private TRADE_ERROR_STATE errState;

    private long buyAbleMoney;

    private CommonBuyConfig(Context mContext) {
        this.mContext = mContext;
        errState = TRADE_ERROR_STATE.ERROR_UNDEFINED;
        buyAbleMoney = 0;
    }

    public static CommonBuyConfig getInstance(Context mContex) {
        if (instance == null) {
            synchronized (CommonBuyConfig.class) {
                if (instance == null) {
                    instance = new CommonBuyConfig(mContex);
                }
            }
        }
        return instance;
    }


    @Override
    public Object getTradeTimeParams() {
        return ConfigUtils.trigTime("09:30","09:32");
    }

    @Override
    public Object getTradderTriggerParams() {
        JSONObject param = new JSONObject();
        try {
            param.put(KEY_MinIndex, GlobalIndexHelper.LOWEST_BUYER_INDEX);
            param.put(KEY_MaxPosition,GlobalIndexHelper.getInstance(mContext).getExptectPosition());
            param.put(KEY_MinMoney,5000);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return param;
    }

    @Override
    public Object getStkCodeParams() {
        return null;
    }

    @Override
    public boolean getIsAutoTrade() {
        return SharePreferenceUtils.getBoolSP(mContext, SharePrefConstants.IS_AUTO_BUY_MODE,true);
    }

    @Override
    public Object getTotalMoneyParams() {
        return null;
    }


    @Override
    public boolean isInTradeTime(Object param) {
        if (param instanceof HashMap){
            return ConfigUtils.isTimeInConfigRange((Map<String, String>) param);
        }
        return false;
    }

    @Override
    public boolean tradeTrigger(Object param) {
        if (param instanceof JSONObject){
            try {
                double minIndex = ((JSONObject) param).getDouble(KEY_MinIndex);
                double maxPosition = ((JSONObject) param).getDouble(KEY_MaxPosition);
                long minMoney = ((JSONObject) param).getLong(KEY_MinMoney);
                if (GlobalIndexHelper.getInstance(mContext).getBuyPercent() > minIndex){
                    if (PositionDao.getInstance(mContext).getCurPositionValue() < maxPosition){
                        buyAbleMoney = PositionDao.getInstance(mContext).getBuyAbleMoney((float) maxPosition);
                        if (buyAbleMoney > minMoney){
                            return true;
                        }else {
                            errState = ERROR_NOT_ENOUGH_MONEY;
                        }
                    }else {
                        errState = ERROR_BEYOND_BUYABLE_POSITION;
                    }
                }else {
                    errState = TRADE_ERROR_STATE.ERROR_UNDER_GLOBAL_INDEX;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public List<TraderObject> tradeCode(Object param) {
        StrategyFundModel fundModel = StrategyFundSelDao.getInstance(mContext).queryBuyModelByOffsetDays(new Date(), 5);
        if (fundModel == null)
            return null;
        TraderObject obj = new TraderObject(fundModel);
        List<TraderObject> objs = new ArrayList<TraderObject>();
        objs.add(obj);
        return objs;
    }


    @Override
    public long tradeTotalMoney(Object param) {
        return buyAbleMoney;
    }


    @Override
    public TraderError generateTriggerErrorState() {
        TraderError error = TraderError.getError(errState);
        return error;
    }



}
